﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NPMonitor.Implementation;
using Newtonsoft.Json;

namespace NPMonitor.Web.Handler
{
    /// <summary>
    /// Summary description for UsersHandler
    /// </summary>
    public class UsersHandler : IHttpHandler
    {

        protected HttpRequest Request
        {
            get
            {
                return HttpContext.Current.Request;
            }
        }

        protected HttpResponse Response
        {
            get
            {
                return HttpContext.Current.Response;
            }
        }

        private HttpContext context;

        public string GetParamsValue(string key)
        {
            if (string.IsNullOrEmpty(Request.QueryString[key]))
                return "";
            else
                return Request.QueryString[key].Trim();
        }

        public void ProcessRequest(HttpContext context)
        {
            this.context = context;
            context.Response.ContentType = "text/plain";
            string operation = GetParamsValue("op");
            switch (operation)
            {
                case "CheckAccountName":
                    ValidateAccountName();
                    break;
                case "CheckUserName":
                    ValidateUserName();
                    break;
                case "CheckPoolName":
                    CheckPoolName();
                    break;
                case "CheckPoolMachine":
                    CheckPoolMachine();
                    break;
            }
        }

        public void CheckPoolName()
        {
            int poolId = Convert.ToInt32(GetParamsValue("poolId").Trim());
            string poolName = context.Request.Params["poolName"].Trim();
            bool result = StoreManager.Current.MachineStore.CheckPool(poolId, poolName);
            string jsonResult = JsonConvert.SerializeObject(result);
            Response.Write(jsonResult);
        }

        public void ValidateAccountName()
        {
            string accountName = GetParamsValue("accountName").Trim();
            int userId = Convert.ToInt32(GetParamsValue("userId").Trim());
            var result = StoreManager.Current.UserStore.CheckAccountName(accountName,userId);
            string jsonresult = JsonConvert.SerializeObject(result);
            Response.Write(jsonresult);
        }

        public void ValidateUserName()
        {
            string username = GetParamsValue("userName").Trim();
            int userId = Convert.ToInt32(GetParamsValue("userId").Trim());
            var result = StoreManager.Current.UserStore.CheckUserName(username,userId);
            string jsonresult = JsonConvert.SerializeObject(result);
            Response.Write(jsonresult);
        }

        public void CheckPoolMachine()
        {
            int poolId = Convert.ToInt32(GetParamsValue("PoolId").Trim());
            var result = StoreManager.Current.MachineStore.CheckPoolMachine(poolId);
            string jsonresult = JsonConvert.SerializeObject(result);
            Response.Write(jsonresult);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}